package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Wallet;
import com.example.demo.entity.WalletDetails;
import com.example.demo.mapper.WalletMapper;
import com.example.demo.service.IWalletService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hjh
 * @since 2023-02-15
 */
@Service
public class WalletServiceImpl extends ServiceImpl<WalletMapper, Wallet> implements IWalletService {

    @Autowired
    private WalletDetailsServiceImpl walletDetailsService;


    @Override
    public Wallet getById1(Long id) {
        // 查询数据库中是否存在用户
        Wallet wallet = this.getById(id);

        System.out.println("===========" +wallet);
        // 用户不存在则创建用户
        if(wallet == null){
            wallet = new Wallet();
            wallet.setBalance(new BigDecimal(0));
            wallet.setId(id);
            wallet.setUsername(id.toString());
            this.save(wallet);
        }

        // 返回用户钱包数据
        return wallet;

    }

    @Override
    @Transactional
    public String xiaofei(Long id, BigDecimal jine) {
        // 获取钱包信息
        Wallet wallet = this.getById(id);

        // 计算当前钱包金额
        BigDecimal newBalance = wallet.getBalance().subtract(jine);

        // 如果小于0，则消费失败
        if(newBalance.compareTo(new BigDecimal(0)) < 0){
            return "消费失败，钱包余额不足，当前金额为："+wallet.getBalance()+",消费金额为："+jine;

        }else {
            // 新增消费明细，插入钱包明细表
            WalletDetails walletDetails = new WalletDetails();
            walletDetails.setType(1);
            walletDetails.setWalletId(id);
            walletDetails.setChangeBalance(jine);
            walletDetails.setBalance(newBalance);
            walletDetailsService.save(walletDetails);

            // 更新钱包金额
            wallet.setBalance(newBalance);
            this.updateById(wallet);

            return "消费成功，消费后钱包余额为"+newBalance+",消费金额为："+jine;
        }
    }

    @Override
    public String add(Long id, BigDecimal jine) {
        // 获取钱包信息
        Wallet wallet = this.getById(id);

        // 计算当前钱包金额
        BigDecimal newBalance = wallet.getBalance().add(jine);


        // 新增退款或提现明细，插入钱包明细表
        WalletDetails walletDetails = new WalletDetails();
        walletDetails.setType(0);
        walletDetails.setWalletId(id);
        walletDetails.setChangeBalance(jine);
        walletDetails.setBalance(newBalance);
        walletDetailsService.save(walletDetails);

        // 更新钱包金额
        wallet.setBalance(newBalance);
        this.updateById(wallet);

        return "提现或退款成功，消费后钱包余额为"+newBalance+",提现或退款金额为："+jine;

    }

    @Override
    public String mingxi(Long id) {
        // 获取钱包明细
        LambdaQueryWrapper<WalletDetails> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WalletDetails::getWalletId,id);
        queryWrapper.orderByDesc(WalletDetails::getUpdateTime);
        List<WalletDetails> list = walletDetailsService.list(queryWrapper);
        return list.toString();
    }


}
